// sub/user/user-invite/user-invite.js
import painter from '../../../utils/painterImage/image.js'

let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    template: {},
    shareImage: '',
    refRegisterShareImgUrl:'',
  },
  // 获取背景图接口
  getBackImg(){
    let _this = this
    let user = app.getUser();
    app.requestUrl({
      method: 'get',
      url: '/wxapi/global/share',
      data: {
        centerId: app.globalData.loginCentreId,
      },
      header: {
        'Content-Type': 'application/json;charset=UTF-8',
        'Authorization': wx.getStorageSync('authToken') || 'empty-token'
      },
    }).then(res => {
      console.log(res.data.baseData);
      _this.setData({
        refRegisterShareImgUrl:res.data.baseData.refRegisterShareImgUrl
      })
    })

  },
  //获取二维码接口
  getQr () {
    let _this = this
    let user = app.getUser()
    let path = 'pages/index/index'
    app.requestUrl({
      method: 'POST',
      url: '/wxapi/qr/appCode',
      data: {
        flag: 3,
        url: path,
        upShopId: user.upshopTid,
        upCentreId: '',
        goodsId: '',
        centreId: app.globalData.loginCentreId,
        cd: '',
        userId: user.id,
        typeFlag:1,
      },
      header: {
        'Content-Type': 'application/json;charset=UTF-8',
        'Authorization': wx.getStorageSync('authToken') || 'empty-token'
      },
    }).then(res => {
      let tempQr = res.data.date
      _this.setData({
        template: new painter().shopShare3(_this.data.refRegisterShareImgUrl,
          '', user.nickname, user.avatar, tempQr),
      })
//      //图片如果是https://wx.qlogo.cn前缀，需要下载到本地，才能绘画
//      let tempAvatar = wx.getStorageSync('tempUserAvatar') || user.avatar;
//      let aa = wx.getStorageSync('tempUserAvatar');
//
//      if (tempAvatar.indexOf('wx.qlogo.cn') != -1) {//indexOf方法，没有目标字符返回-1；去下载头像并保存到缓存
//        wx.downloadFile({
//          url: user.avatar,
//          success: function (res) {
//            wx.setStorageSync('tempUserAvatar',res.tempFilePath)//把下载的本地头像保存到缓存
//            tempAvatar = res.tempFilePath;
//          },
//          fail: function(res) {
//            wx.showToast({
//              title: '下载失败',
//              icon: 'none',
//            })
//          },
//          complete: function(res) {
//            _this.setData({
//              template: new painter().shopShare3('https://o.rjkj7.com/data/upload/xcximages/qr_back_1.png',
//                'https://o.rjkj7.com/data/upload/xcximages/qr_back_2.png', user.nickname, tempAvatar, tempQr),
//            })
//          },
//        })
//      } else {
//        _this.setData({
//          template: new painter().shopShare3('https://o.rjkj7.com/data/upload/xcximages/qr_back_1.png',
//            'https://o.rjkj7.com/data/upload/xcximages/qr_back_2.png', user.nickname, tempAvatar, tempQr),
//        })
//      }
    })

  },

  onImgOK (e) {
    this.setData({
      shareImage: e.detail.path
    })
  },
  onImgErr(e) {
    console.log(e)
    wx.showToast({
      title: '生成失败,请刷新页面重试',
      icon: 'none'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getBackImg();
    this.getQr()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var _this = this
    let user = app.getUser()
    return {
      title: user.nickname + ',邀请你注册',
      path: '/pages/index/index?heShopId=' + app.getShop().id + '&heCentreId=' + app.globalData.loginCentreId + '&heMid=' + user.id,
      success: function () {

      },
      fail: function () {
        wx.showToast({title: '分享失败', icon: 'none'})
      }
    }
  }
})
